/*************************************************
 *FileName:      complaint.js
 *Description:   投诉.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: false           //是否启用回弹 默认true
    });
    /*
     * 变量初始化
     */
    var that =getUrlParam('thatid');
    var orid =getUrlParam('orid');
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        $('.complaint .complaint-add').on('tap',function(){
            captureImage();
        })//投诉页面添加图片
        
        $('.complaint-img-box').on('tap','.close',function(){
            if($('.complaint-img-box li').length == 5){$('.complaint-add').parent().css({display:'inline-block'})}
            $(this).parent().remove();
        })//删除照片
        
        $('.complaint-text').on('input',function(){
            $('.shu').text($(this).val().length+'/100')
        })
        
    }
    /**
     * captureImage
     * 拍照和相册 
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function captureImage(){
        common.actionSheet('选择上传','取消',[{title:'从相册获取'},{title:'拍照'}],function(index){
            if(index==1){
                if($('.addVideo').length){
                    mui.toast('不能同时上传图片和视频',{ duration:2000, type:'div' });
                }else{
                    common.galleryImg(function(src){
                        //上传
                        if($('.complaint-img-box li').length>3 ){$('.complaint-add').parent().css({display:'none'})} ;
                        $('.complaint-add').parent().before('<li><a class="complaint-img"><img src="'+src+'"/></a><i class="close"></i></li>');
                    })
                }
            }else if(index==2){
                if($('.addVideo').length){
                    mui.toast('不能同时上传图片和视频',{ duration:2000, type:'div' });
                }else{
                    common.cameraImg(function(src){
                        if($('.complaint-img-box li').length>3 ){$('.complaint-add').parent().css({display:'none'})} ;
                        $('.complaint-add').parent().before('<li><a class="complaint-img"><img src="'+src+'"/></a><i class="close"></i></li>');
                    })
                }
            } 
        })
    }
    /**
     * 
     * 提交
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    $('.rdntn').on('tap',function(){
        var content =$('.complaint-text').val();
        var src1=[],src2=[],src3=[],src4=[];
        src1 =$('.complaint-img-box li').eq(0).find('img').attr('src');
        src2 =$('.complaint-img-box li').eq(1).find('img').attr('src');
        src3 =$('.complaint-img-box li').eq(2).find('img').attr('src');
        src4 =$('.complaint-img-box li').eq(3).find('img').attr('src');
        if(content.length>100){
            mui.toast('请输入100以内的字数！');
            return false;
        }else{
            var task = plus.uploader.createUpload(DOMAIN_NAME + "/index.php/api/user/complaintsAdd",
            { method:"POST",blocksize:20480,priority:100 },
                function (t, status) { //上传完成
                    common.loadingOut();
                    if (status == 200) {
                        mui.toast('上传成功',{ duration:2000, type:'div' });
                        setTimeout(function(){mui.back()},500);
                    }else{ 
                        mui.toast('上传失败',{ duration:2000, type:'div' });
                    }
                }
            );
            common.loadingIn();
            task.addData("uid", uid);
            task.addData("token", token);
            task.addData("complaint_uid", that);
            task.addData("content", content);
            if(orid){
                task.addData("type", "2");
            }else{
                task.addData("type", "1");
            }
            task.addFile(src1, {key: "img1"});
            task.addFile(src2, {key: "img2"});
            task.addFile(src3, {key: "img3"});
            task.addFile(src4, {key: "img4"});
            mui.toast('开始上传',{ duration:2000, type:'div' });
            task.start();
        }
    })
